
/* global document */
// TODO 查询行
import React from 'react'
import PropTypes from 'prop-types'
import moment from 'moment'
import classnames from 'classnames'
import {FilterItem} from 'components'
import {Form, Button, Radio, Row, Col, DatePicker, Input, Cascader, Switch} from 'antd'
import city from 'utils/city'

const {Search} = Input
const {RangePicker} = DatePicker
const {RadioGroup} = Radio.Group

const ColProps = {
  xs: 24,
  sm: 12,
  style: {
    marginBottom: 16,
  },
}

const TwoColProps = {
  ...ColProps,
  xl: 96,
}

const LinkPubFilter = ({
                      onAdd,
                      isMotion,
                      onFilterChange,
                      switchLinkPubFilter,
                      filter,
                      form: {
                        getFieldDecorator,
                        getFieldsValue,
                        setFieldsValue,
                      },
                    }) => {
  const handleFields = (fields) => {
    // const {createTime} = fields
    // if (createTime.length) {
    //   fields.createTime = [createTime[0].format('YYYY-MM-DD'), createTime[1].format('YYYY-MM-DD')]
    // }
    return fields
  }

  const handleSubmit = (v) => {
    let fields = getFieldsValue()
    fields = handleFields(fields)
    if (v === 'region' || v === 'role') {
      fields.linkPubFilterType = v
    }
    onFilterChange(fields)
  }

  const handleReset = () => {
    const fields = getFieldsValue()
    for (let item in fields) {
      if ({}.hasOwnProperty.call(fields, item)) {
        if (fields[item] instanceof Array) {
          fields[item] = []
        } else {
          fields[item] = undefined
        }
      }
    }
    setFieldsValue(fields)
    handleSubmit()
  }

  const handleChange = (key, values) => {
    let fields = getFieldsValue()
    fields[key] = values
    fields = handleFields(fields)
    onFilterChange(fields)
  }
  const {name, address} = filter

  let initialCreateTime = []
  if (filter.createTime && filter.createTime[0]) {
    initialCreateTime[0] = moment(filter.createTime[0])
  }
  if (filter.createTime && filter.createTime[1]) {
    initialCreateTime[1] = moment(filter.createTime[1])
  }

  const switchLinkPubFilterToRegion = () => {
    window.localStorage.setItem("linkPubFilterType", "region")
    handleSubmit("region")
  }

  const switchLinkPubFilterToRole = () => {
    window.localStorage.setItem("linkPubFilterType", "role")
    handleSubmit("role")
  }

  return (
    <Row gutter={4}>

      {/*链接查询*/}
      <Col {...ColProps} xl={{span: 8}} md={{span: 16}}>
         {
           getFieldDecorator('title', {initialValue:name})(
              <Search placeholder = "输入链接标题"  enterButton = "搜索" onSearch = {handleSubmit} />
            )
          }
      </Col>

      {/*查询按钮*/}
      <Col {...TwoColProps} xl={{span: 16}} md={{span: 24}} sm={{span: 24}}>
        <div style={{display: 'flex', justifyContent: 'space-between', flexWrap: 'wrap'}}>

          <div>
            {/*<Button type="primary" className="margin-right" onClick={handleSubmit}>查询</Button>*/}
            <Button onClick={handleReset} style= {{marginLeft:16 }}>重置</Button>
          </div>

          <div className="flex-vertical-center">
            {/*单选按钮*/}
            <Button onClick={switchLinkPubFilterToRegion} type="ghost" style= {{marginLeft:16 }} >按单位查询</Button>
            <Button onClick={switchLinkPubFilterToRole} type="ghost" style= {{marginLeft:16 }} >按角色查询</Button>
            {/* <RadioGroup onChange={e => switchLinkPubFilter(e.target.value)} >
              <Radio value={"region"}>单位</Radio>
              <Radio value={"role"}>角色</Radio>
            </RadioGroup> */}
          </div>
        </div>
      </Col>
    </Row>
  )
}

LinkPubFilter.propTypes = {
  onAdd: PropTypes.func,
  isMotion: PropTypes.bool,
  form: PropTypes.object,
  filter: PropTypes.object,
  onFilterChange: PropTypes.func,
}

export default Form.create()(LinkPubFilter)
